class Ownership < ActiveRecord::Base
	belongs_to :user
	belongs_to :stock
	validates_numericality_of :quantity
	def self.my_stocks (user_id)
		find(	:all,
				:conditions => "user_id = #{user_id}" )
	end
	def self.instance (user_id, stock_id)
		first(	:conditions => "user_id = #{user_id} AND stock_id = #{stock_id}" )
	end
	def self.details(id)
		stock = Stock.find(id)
		return stock.name, stock.market_value
	end	
	def self.owns(user_id, stock_id)
		ownership = first(	:conditions => "user_id = #{user_id} AND stock_id = #{stock_id}" )
		if ownership.nil?
			return 0
		else
			return ownership.quantity
		end
	end
	protected
	def validate
	  errors.add(:quantity, "should be positive") unless quantity.nil? || quantity > 0.0
	end
end
